/**
 *	@file	radio_aoi.c
 *	@brief	ラジオ　バラエティチャンネル　アオイの合言葉データ生成
 *	@author	Miyuki Iwasawa
 *	@date	09.04.17
 */

#include "common.h"
#include "system/procsys.h"
#include "system/arc_util.h"
#include "system/arc_tool.h"
#include "system/bmp_list.h"
#include "system/bmp_menu.h"
#include "system/pm_str.h"
#include "system/msgdata.h"
#include "system/fontproc.h"
#include "system/snd_tool.h"
#include "system/buflen.h"
#include "system/wordset.h"
#include "system/pm_rtc.h"
#include "system/snd_radio.h"
#include "itemtool/item.h"
#include "itemtool/myitem.h"
#include "application/app_tool.h"
#include "msgdata/msg.naix"

#include "field/evwkdef.h"
#include "field/syswork.h"
#include "fielddata/script/saveflag.h"
#include "savedata/zukanwork.h"
#include "radio_prog.h"
#include "msgdata/msg_pg_radio.h"
#include "msgdata/msg_radio_p_aoi.h"
#include "msgdata/msg_d23r0102.h"

typedef struct{
 int heapID;
 u16 seq;
 u16 msg_id;
}RADIO_AOI_WORK;

static void aoi_ResInit(PM_RADIO_MAN* man);
static void aoi_ResRelease(PM_RADIO_MAN* man);

/**
 *	@brief	ラジオ　アオイの合言葉　初期化
 */
int RadioAoi_Init(PM_RADIO_MAN* man)
{
	RADIO_AOI_WORK* wk = NULL;

	wk = sys_AllocMemory(man->heapID,sizeof(RADIO_AOI_WORK));
	MI_CpuClear8(wk,sizeof(RADIO_AOI_WORK));

	man->ch_wk = (void*)wk;

	aoi_ResInit(man);

	//再生開始
	Snd_BgmStop(Snd_NowBgmNoGet(),0);
	RADIO_PLAY(RADIO_BGM_AOI);
//	PMGS_PlayRadio(RADIO_BGM_AOI);

	return 0;
}

/**
 *	@brief	ラジオ　アオイの合言葉 解放
 */
int RadioAoi_End(PM_RADIO_MAN* man)
{
	aoi_ResRelease(man);

	MI_CpuClear8(man->ch_wk,sizeof(RADIO_AOI_WORK));
	sys_FreeMemoryEz(man->ch_wk);
	man->ch_wk = NULL;

	return 0;
}

/**
 *	@brief	ラジオ　アオイの合言葉 再生メイン
 */
int RadioAoi_Play(PM_RADIO_MAN* man)
{
	RADIO_AOI_WORK* wk = (RADIO_AOI_WORK*)man->ch_wk;

	switch(wk->seq){
	case 0:	//タイトルコール
		RadioSub_SentencePlay(man,m_aoi_00_01);
		wk->seq++;
		break;
	case 1:
		if(!RadioSub_SentencePlayWait(man)){
			break;
		}
		//本編再生
		RadioSub_SentencePlay(man,wk->msg_id);
		//合言葉を聞いたフラグを立てておく
		if(wk->msg_id == m_aoi_01_01){	//ブルーカード持ってる
			EventWork_SetEventFlag( SaveData_GetEventWork(man->save), TMFLG_AOI_AIKOTOBA_CHECK );
		}
		wk->seq++;
		break;
	case 2:
		if(!RadioSub_SentencePlayWait(man)){
			break;
		}
		wk->seq++;
		break;
	case 3:
		if(!RadioSub_MsgWaitScroll(man)){
			break;
		}
		//アイキャッチ
		RadioSub_EyeCatchPlay(man,m_aoi_00_02);
		wk->seq++;
		break;
	case 4:
		if(!RadioSub_EyeCatchPlayWait(man)){
			break;
		}
		//CMリクエスト
		man->cm_req_f = TRUE;
		return 1;
	}
	return 0;
}

/*
 *	@brief	アオイの合言葉用 必要なデータを初期化する
 */
static void aoi_ResInit(PM_RADIO_MAN* man)
{
	u16 id;
	MSGDATA_MANAGER* msg;
	STRBUF* word;
	RADIO_AOI_WORK* wk = (RADIO_AOI_WORK*)man->ch_wk;

	man->msg_prog = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,NARC_msg_radio_p_aoi_dat,man->heapID);

	//番組タイトルとDJ名を通知
	MSGMAN_GetString(man->msg_prog,m_aoi_title,man->str_title);
	MSGMAN_GetString(man->msg_prog,m_aoi_dj,man->str_dj);

	//ブルーカードを持っているか？
	if(MyItem_CheckItem( SaveData_GetMyItem(man->save), ITEM_BURUUKAADO, 1, man->heapID )){
		wk->msg_id = m_aoi_01_01;	//持ってる
	}else{
		wk->msg_id = m_aoi_01_02;
	}

	msg = MSGMAN_Create(MSGMAN_TYPE_NORMAL,ARC_MSG,NARC_msg_d23r0102_dat,man->heapID);

	id = (SysWork_AoiAikotobaGet( SaveData_GetEventWork(man->save)))%30;	//念のため%30しておく
	word = MSGMAN_AllocString(msg,msg_aoi_aikotoba_01+id);

	WORDSET_RegisterWord( man->wset,0,word,PM_NEUTRAL, TRUE, PM_LANG);

	STRBUF_Delete(word);
	MSGMAN_Delete(msg);
}

/**
 *	@brief	アオイの合言葉用　リソース解放
 */
static void aoi_ResRelease(PM_RADIO_MAN* man)
{
	RADIO_AOI_WORK* wk = (RADIO_AOI_WORK*)man->ch_wk;

	MSGMAN_Delete(man->msg_prog);
}
